/**
 * 
 */
package com.abso.mp3tunes.locker.ui.viewers;

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import com.abso.mp3tunes.locker.core.LockerServiceException;
import com.abso.mp3tunes.locker.core.data.Album;
import com.abso.mp3tunes.locker.core.data.Artist;
import com.abso.mp3tunes.locker.core.data.LockerDataUtils;
import com.abso.mp3tunes.locker.core.data.Playlist;
import com.abso.mp3tunes.locker.core.data.Track;
import com.abso.mp3tunes.locker.ui.LockerImages;

/**
 * An abstract base class for label providers used by Locker tree viewers.
 */
public abstract class LockerViewerLabelProvider extends LabelProvider {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
	 */
	public String getText(Object element) {
		if (element instanceof Track) {
			return getTrackText((Track) element, true, false);
		} else if (element instanceof Album) {
			return getAlbumText((Album) element, true);
		} else if (element instanceof Artist) {
			return getArtistText((Artist) element, true);
		} else if (element instanceof Playlist) {
			return getPlaylistText((Playlist) element);
		} else if (element instanceof LockerServiceException) {
			return ((LockerServiceException) element).getMessage();
		} else {
			return super.getText(element);
		}
	}

	/**
	 * Returns the label's text for a track.
	 * 
	 * @param track
	 *            the track whose label's text must be returned.
	 * @param showNumber
	 *            if <code>true</code>, the text includes the track's number.
	 * @param showArtistName
	 *            if <code>true</code>, the text includes the artist's name.
	 * @return the track's text.
	 */
	protected String getTrackText(Track track, boolean showNumber,
			boolean showArtistName) {
		StringBuffer text = new StringBuffer();
		if (showNumber) {
			text.append(track.getNumber());
			text.append(". ");
		}
		text.append(track.getTitle());
		if (showArtistName) {
			text.append(" - ");
			text.append(track.getArtistName());
		}
		text.append(" [");
		text.append(LockerDataUtils.getPrettyPrintedLength(track.getLength()));
		text.append("]");
		return text.toString();
	}

	/**
	 * Returns the label's text for an album.
	 * 
	 * @param album
	 *            the album whose label's text must be returned.
	 * @param showArtist
	 *            if <code>true</code>, the text includes the artist's name.
	 * @return the album's text.
	 */
	protected String getAlbumText(Album album, boolean showArtist) {
		StringBuffer text = new StringBuffer();
		text.append(album.getTitle());
		if (showArtist) {
			text.append(" - ");
			text.append(album.getArtistName());
		}
		return text.toString();
	}

	/**
	 * Returns the label's text for an artist.
	 * 
	 * @param artist
	 *            the artist whose label's text must be returned.
	 * @param showAlbumCount
	 *            if <code>true</code>, the text includes the number of
	 *            albums of the specified artist.
	 * @return the artist's text.
	 */
	protected String getArtistText(Artist artist, boolean showAlbumCount) {
		StringBuffer text = new StringBuffer();
		text.append(artist.getName());
		if (showAlbumCount) {
			text.append(" (");
			text.append(artist.getAlbumCount());
			if (artist.getAlbumCount() == 1) {
				text.append(" album");
			} else {
				text.append(" albums");
			}
			text.append(")");
		}
		return text.toString();
	}

	/**
	 * Returns the label's text for a playlist.
	 * 
	 * @param playlist
	 *            the playlist whose label's text must be returned.
	 * @return the playlist's text.
	 */
	private String getPlaylistText(Playlist playlist) {
		StringBuffer text = new StringBuffer();
		if ("RANDOM_TRACKS".equals(playlist.getId())) {
			text.append("Random"); // avoid repetition of the word 'track'
		} else if ("NEWEST_TRACKS".equals(playlist.getId())) {
			text.append("Newest"); // avoid repetition of the word 'track'
		} else {
			text.append(playlist.getTitle());
		}
		text.append(" (");
		if (playlist.getFileCount() == 0) {
			text.append("No tracks");
		} else if (playlist.getFileCount() == 1) {
			text.append("1 track");
		} else {
			text.append(playlist.getFileCount());
			text.append(" tracks");
		}
		text.append(")");
		return text.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
	 */
	public Image getImage(Object element) {
		if (element instanceof Track) {
			return LockerImages.get(LockerImages.TRACK);
		} else if (element instanceof Album) {
			return LockerImages.get(LockerImages.ALBUM);
		} else if (element instanceof Artist) {
			return LockerImages.get(LockerImages.ARTIST);
		} else if (element instanceof Playlist) {
			return LockerImages.get(LockerImages.PLAYLIST);
		} else if (element instanceof PendingNode) {
			return LockerImages.get(LockerImages.PENDING);
		} else if (element instanceof LockerServiceException) {
			ISharedImages sharedImages = PlatformUI.getWorkbench()
					.getSharedImages();
			return sharedImages.getImage(ISharedImages.IMG_OBJS_ERROR_TSK);
		} else {
			return super.getImage(element);
		}
	}

}